<script setup>

import {selectPage, addGateway, updateGateway, deleteGateway} from '@/api/system/apartment/wegateway'


export default {
  data() {
    return {
      dialogVisible: false,
      queryParams: {
        gatewayName: "",
        pageNum: 1,
        pageSize: 3
      },
      total: 1,
      title: "",
      tableData: [{
        id: "1",
      }],
      ruleForm: {
        id: "",
        gatewayName: '',
        gatewayPort: '',
        gatewayIp: '',
        heartBeatBody: "",
        account: "",
        password: ""
      },
      rules: {
        gatewayName: [
          {required: true, message: '请输入网关名称', trigger: 'blur'},
        ],
        gatewayPort: [
          {required: true, message: '请输入网关端口', trigger: 'blur'},
        ],
        gatewayIp: [
          {required: true, message: '请输入网关ip', trigger: 'blur'},
        ]
      }
    }
  }, methods: {
    resetQuery() {
      this.queryParams = {};
      this.resetForm("queryParams");
      this.queryMethods();
    },
    queryMethods() {
      let that = this;
      selectPage(that.queryParams).then(res => {
        that.tableData = res.data.list;
        that.queryParams.pageNum = res.data.pageNum;
        that.queryParams.pageSize = res.data.pageSize;
        that.total = res.data.total;
      })
    },
    addGatewayShow() {
      this.title = "添加网关信息"
      this.ruleForm = {
        id: "",
        gatewayName: '',
        gatewayPort: '',
        gatewayIp: '',
        heartBeatBody: "",
        account: "",
        password: ""
      };
      this.dialogVisible = true;
    },
    updateGatewayShow(row) {
      this.title = "修改网关信息"
      this.ruleForm = row;
      this.dialogVisible = true;
    },
    submitForm(formName) {
      let that = this;
      that.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title == "添加网关信息") {
            addGateway(that.ruleForm).then(res => {
              if (res.code == 200) {
                that.$message({
                  message: '添加成功',
                  type: 'success'
                });
                that.dialogVisible = false;
                that.queryMethods();
              } else {
                that.$message({
                  message: "添加失败",
                  type: 'error'
                });
              }
            })
          } else if (that.title == "修改网关信息") {
            updateGateway(that.ruleForm).then(res => {
              if (res.code == 200) {
                that.$message({
                  message: '修改成功',
                  type: 'success'
                });
                that.dialogVisible = false;
                that.queryMethods();
              } else {
                that.$message({
                  message: "修改失败",
                  type: 'error'
                });
              }
            })
          }

        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSizeChange(pageSize) {
      this.queryParams.pageSize = pageSize;
      this.queryMethods();
    },
    handleCurrentChange(pageNum) {
      this.queryParams.pageNum = pageNum;
      this.queryMethods();
    },
    deleteGateway(id) {
      let that = this;
      that.$confirm('此操作将永久删除该网关配置吗?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGateway(id).then(res => {
          if (res.code == 200) {
            that.$message({
              message: '删除成功',
              type: 'success'
            });
            that.queryMethods();
          } else {
            that.$message({
              message: res.msg,
              type: 'error'
            });
          }
        })
      }).catch(() => {
        that.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  created() {
    this.queryMethods();
  }
}
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true">
      <el-form-item label="网关名称">
        <el-input
          v-model="queryParams.gatewayName"
          placeholder="请输入网关名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryMethods()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" style="margin-top:30px;margin-bottom: 30px" icon="el-icon-plus"
               @click="addGatewayShow">
      新增网关
    </el-button>

    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="gatewayName"
        label="网关名称"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="gatewaySerialNumber"
        label="网关序列号"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="gatewayIp"
        label="网关ip"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="gatewayPort"
        label="网关端口"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="网关状态"
        align="center"
        width="180">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.gatewayStatus==1">在线</el-tag>
          <el-tag type="danger" v-if="scope.row.gatewayStatus==0">离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="joinTime"
        label="入网时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200">
        <template slot-scope="scope">
<!--          <el-tooltip content="刷新状态" placement="top">-->
<!--            <el-button type="success" icon="el-icon-refresh" @click="updateGatewayShow(scope.row.id)"-->
<!--                       circle></el-button>-->
<!--          </el-tooltip>-->
          <el-tooltip content="编辑" placement="top">
            <el-button type="primary" icon="el-icon-edit" @click="updateGatewayShow(scope.row)" circle></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button type="danger" icon="el-icon-edit" @click="deleteGateway(scope.row.id)" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryParams.pageNum"
        :page-size="this.queryParams.pageSize"
        :page-sizes="[1,2,3]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
               class="demo-ruleForm">
        <el-form-item label="网关名称" prop="gatewayName">
          <el-input v-model="ruleForm.gatewayName"></el-input>
        </el-form-item>
        <el-form-item label="网关序列号" prop="gatewaySerialNumber">
          <el-input v-model="ruleForm.gatewaySerialNumber"></el-input>
        </el-form-item>
        <el-form-item label="网关IP" prop="gatewayIp">
          <el-input v-model="ruleForm.gatewayIp"></el-input>
        </el-form-item>
        <el-form-item label="网关端口" prop="gatewayPort">
          <el-input v-model="ruleForm.gatewayPort"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="心跳包内容" prop="heartBeatBody">
          <el-input style="width: 500px" v-model="ruleForm.heartBeatBody"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">操作</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">


</style>
